package com.yyfax.common.form.interaction;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yyfax.common.R;
import com.yyfax.common.animation.ResizeWidthAnimation;
import com.yyfax.common.tools.DensityUtil;


/**
 * Created by fan on 2017/4/1.
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class SimpleNormalInteraction extends SearchBarInteraction implements View.OnClickListener, TextView.OnEditorActionListener {

	private final int mSpace;
	private EditText mSearchContentEt;
	private final static int LEFT = 0;
	private final static int RIGHT = 1;
	private int mDirection = RIGHT;
	private CharSequence mSearchContent;
	private View msearchContainer;
	private int mMarginLeft = -1;
	private boolean mFirstAnim = true;

	public SimpleNormalInteraction(Context context, TypedArray ta, View view) {
		super(context, ta, view);
		mSpace = DensityUtil.dip2px(context, 49);
	}

	@Override
	protected void initView(Context context, TypedArray ta, View view) {
		mSearchContentEt = (EditText) view.findViewById(R.id.search_content_et);
		msearchContainer = view.findViewById(R.id.search_container);
		View cancelBtn = view.findViewById(R.id.cancel_btn);
		mSearchContentEt.setOnClickListener(this);
		msearchContainer.setOnClickListener(this);
		cancelBtn.setOnClickListener(this);
		mSearchContentEt.setOnEditorActionListener(this);

	}

	@Override
	protected void setListenerToShadowView() {
		mShadowView.setOnClickListener(this);
	}

	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
			String criteria = mSearchContentEt.getText().toString().trim();
			if (TextUtils.isEmpty(criteria)) {
				Toast.makeText(mContext, R.string.search_criteria_empty, Toast.LENGTH_SHORT).show();
				return false;
			}
			if (mFillSearchContent) {
				mSearchContent = mSearchContentEt.getText().toString();
			}
			setOriginalState();
			if (mSearchBarListener != null) {
				mSearchBarListener.searchContent(NO_TYPE, criteria);
			}
			return true;
		}
		return false;
	}

	@Override
	public void setSearchContent(String content) {
		mSearchContent = content;
		mSearchContentEt.setText(content);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.search_container || id == R.id.search_content_et) {
			if (mDirection == RIGHT) {
				startAnimation(LEFT);
			}
			showInputWindow(mSearchContentEt);
			resetShadowState(true);
		} else if (id == R.id.cancel_btn || (mShadowView != null && id == mShadowView.getId())) {
			setOriginalState();
		}
	}

	private void setOriginalState() {
		mSearchContentEt.setText(mFillSearchContent ? mSearchContent : "");
		if (mDirection == LEFT) {
			startAnimation(RIGHT);
		}
		resetShadowState(false);
		hideInputWindow(mSearchContentEt);
	}

	private void startAnimation(int direction) {
		mDirection = direction;
		ResizeWidthAnimation anim = new ResizeWidthAnimation(msearchContainer, direction == LEFT ? mSpace : 0 - mSpace);
		anim.setDuration(300);
		msearchContainer.startAnimation(anim);
		if (!TextUtils.isEmpty(mSearchContent)) {
			return;
		}
		switch (direction) {
			case LEFT:
				Animation a = new Animation() {
					@Override
					protected void applyTransformation(float interpolatedTime, Transformation t) {
						RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mSearchContentEt.getLayoutParams();
						if (mMarginLeft == -1) {
							params.addRule(RelativeLayout.CENTER_IN_PARENT, 0);//false

							int total = msearchContainer.getWidth();
							String hint = mSearchContentEt.getHint().toString();
							int width = getTextWidth(hint);
							mMarginLeft = ((total - width) / 2) - 100;
//							CLog.i(TAG, "total = " + total + ", width = " + width + ", mMarginLeft = " + mMarginLeft);
							params.leftMargin = mMarginLeft;
						} else {
							if (mFirstAnim) {
								if (interpolatedTime < 1) {
									int margin = (int) ((-mMarginLeft) * interpolatedTime);
//									CLog.i(TAG, "mMarginLeft=" + mMarginLeft + ", leftMargin = " + margin + ", interpolated = " + interpolatedTime);
									params.leftMargin = margin;
								} else {
									params.leftMargin = 0;
									params.width = RelativeLayout.LayoutParams.MATCH_PARENT;
								}
							} else {
								int margin = (int) (mMarginLeft - 100 + ((-mMarginLeft) * interpolatedTime));
//								CLog.i(TAG, "FALSE mMarginLeft=" + mMarginLeft + ", leftMargin = " + margin + ", interpolated = " + interpolatedTime);
								params.leftMargin = margin > 0 ? margin : 0;
							}
						}
						mSearchContentEt.setLayoutParams(params);
					}
				};
				a.setDuration(300);
				mSearchContentEt.startAnimation(a);
				break;
			case RIGHT:
				Animation b = new Animation() {
					@Override
					protected void applyTransformation(float interpolatedTime, Transformation t) {
						RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mSearchContentEt.getLayoutParams();
						if (interpolatedTime < 1) {
							int margin = (int) ((mMarginLeft - 100) * interpolatedTime);
//							CLog.i(TAG, "mMarginLeft=" + mMarginLeft + ", leftMargin = " + margin + ", interpolated = " + interpolatedTime);
							params.leftMargin = margin;
						} else {
//							CLog.i(TAG, "interpolated = " + interpolatedTime);
							mFirstAnim = false;
						}

						mSearchContentEt.setLayoutParams(params);
					}
				};
				b.setDuration(300);
				mSearchContentEt.startAnimation(b);
				break;
		}
	}

	private int getTextWidth(String str) {
		Paint paint = new Paint();
		Rect rect = new Rect();
		paint.getTextBounds(str, 0, str.length(), rect);
		int w = rect.width();
		return w;
	}

}
